//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    bannerList: [],
    publicList: [],
    classLevelList: {},
    background: ['red', 'green', 'blue'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500
  },
  goToDetails :function (e){
    console.log(e,"我是e")
    wx.navigateTo({
      url:"../detail/detail?Id="+e.currentTarget.dataset.id
    })
  },
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  bannerListFn: function () {
    let _this=this
    console.log(this)
    wx.request({
      url: 'http://localhost:3000/banner',
      method: 'GET',
      success: function (res) {
        console.log(res)
        _this.setData({ bannerList: res.data.data })
      }
    })
  },
  publicListFn: function () {
    let _this=this
    wx.request({
      url: 'http://localhost:3000/publicList',
      method: 'GET',
      success: function (res) {
        console.log(res)
        _this.setData({ publicList: res.data.data })
      }
    })
  },
  classListFn: function () {
    let _this=this
    wx.request({
      url: 'http://localhost:3000/classList',
      method: 'GET',
      success: function (res) {
        console.log(res)
        _this.setData({ classLevelList: res.data.data })
      }
    })
  },
  onLoad: function () {
    console.log(this)
    this.bannerListFn()
    this.publicListFn()
      this.classListFn()


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
